package webserver.src;

/*
 * This class reads in the MIME.type file and populates an appropriate arraylists.
 * It also skips over blank lines and lines beginning with '#' (comments)
 */
import java.util.ArrayList;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.HashMap;

public class Mimes {    
    private String mType;
    private ArrayList<String> extensionType;
    private String prefix;
    private static HashMap<String, ArrayList<String>> mimeTable;
    private static HashMap<ArrayList<String>, String> revmimeTable;
    
    public Mimes ()
    {
       mimeTable = new HashMap<String, ArrayList<String>>();
       revmimeTable = new HashMap<ArrayList<String>, String>();
       mType = null;
       extensionType = new ArrayList<String>();
       prefix = ".";
    }//end constructor
    
    public void readMimeFile(String thisFile) throws FileNotFoundException, IOException 
    { 
        String inputLine=null;
        String tokens[] = new String [10];
        int size=0;
        //read in file
        BufferedReader mimeReader = new BufferedReader( new FileReader(thisFile));
        while (mimeReader.ready())
        {   //get types
            inputLine = mimeReader.readLine();
            if (!inputLine.startsWith("#") && !inputLine.isEmpty()) {
                //System.out.println(inputLine);
                tokens= inputLine.split(" ");
                mType = tokens[0];
                //System.out.println(tokens[0]);
                //fill arraylist
                size = tokens.length;
                for(int c=1; c<size;c++) 
                {   if (!tokens[c].isEmpty()){
                        extensionType.add(prefix+tokens[c]);
                        //System.out.println(prefix+tokens[c]);
                    }
                }
                
                //populate hashmap
                mimeTable.put(mType, extensionType);
                revmimeTable.put(extensionType, mType);
            }
        }//end while
        
    }//end readMimeFile
    
    
    public static void main ( String args[] )
    {
        Mimes m = new Mimes();
        try{
            m.readMimeFile("C:" + File.separator + 
                           "Users" + File.separator + 
                           "pia" + File.separator +
                           "Desktop" + File.separator + 
                           "MyServer" + File.separator + 
                           "MIME.type");
        } catch (Exception e)
        {System.out.println("Exception cauight: " + e.getMessage() );}
    }//end main
}
